/**
* @Title: Test.java
* @Package com.hibernates
* @Description: TODO(用一句话描述该文件做什么)
* @author antch,861710865@qq.com
* @date 2017年6月27日 下午1:18:11
* @version V1.0
*/

package com.hibernates;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.model.Book;
import com.model.BookStore;
import com.model.Product;
import com.model.Student;

/**
 * @ClassName: Test
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author antch,861710865@qq.com
 * @date 2017年6月27日 下午1:18:11
 *
 */

@SuppressWarnings("deprecation")
public class Test {

	/**
	 * @Title: main @Description: TODO(这里用一句话描述这个方法的作用) @param @param args
	 *         设定文件 @return void 返回类型 @throws
	 */

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Configuration configuration = new Configuration().configure();
		// 创建会话连接工厂
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		// 创建会话
		Session session = sessionFactory.openSession();
		// 开启事务
		Transaction transaction = session.beginTransaction();

		Product p = new Product();
		p.setProductPrice(48);
		session.save(p);

		/*
		 * transaction.commit(); transaction.begin(); p.setProductName("123");
		 * transaction.commit();
		 *
		 */
		// transaction.begin();

		System.out.println("保存的Person對象：" + p);
		p.setProductPrice(67);

		BookStore bookStore = new BookStore();
		/*
		 * bookStore.setBookStoreName("文学书籍"); Book book = new Book();
		 * book.setBookName("平凡的世界2"); book.setBookPrice(31);
		 * book.setBookStore(bookStore);
		 */
		// 保存书架
		/*
		 * session.save(bookStore); session.save(book);
		 *
		 *
		 */
		// session.
		new Test().saveBookStore(session);

		String hql = "From BookStore";
		@SuppressWarnings({ "deprecation", "unchecked" })
		Query<BookStore> query = session.createQuery(hql);

		List<BookStore> list = query.list();
		System.out.println("查询到的数据量：：" + list.size());
		System.out.println(list);
		int i =0 ;
		for (BookStore bookStore2 : query.list()) {

			System.out.println(bookStore2);
			if(i == 0) {
				new Test().deleBookStore(session, bookStore2);
			}
			i++;
			System.out.println();
		}



		session.flush();
		session.close();
		sessionFactory.close();
	}

	public void saveBookStore(Session session) {
		Book b1 = new Book();
		b1.setBookName("java开发");
		b1.setBookPrice(23);
		Book b2 = new Book();
		b2.setBookName("Java web开发");
		b2.setBookPrice(23);
		BookStore bookStore = new BookStore();
		bookStore.setBookStoreName("技术书籍");
		session.save(bookStore);
		b2.setBookStore(bookStore);
		b1.setBookStore(bookStore);
		session.save(b1);
		session.save(b2);
		System.out.println("书籍保存成功");

	}

	public void deleBookStore(Session session,BookStore bookStore) {
		session.delete(bookStore);
		System.out.println("删除成功");
	}

	/**
	 *
	* @Title: saveStudents
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @param @param session    设定文件
	* @return void    返回类型
	* @throws
	 */
	public void saveStudents(Session session) {

		Student student = new Student();
		student.setAge(34);
		/*stu
*/
	}
}
